export function toTree<T extends { children?: T[] }, U extends { children?: U[] }>(
  data: T[],
  callback: (obj: Omit<T, 'children'>, children?: T[]) => U
) {
  const loop = ({ children, ...restProps }: T, inner: U[] | null = null): U => {
    let inn = inner;
    if (children?.length) {
      inn = children.map((item) => loop(item, inner));
    }
    return {
      ...callback(restProps, children),
      children: inn || [],
    };
  };
  return data.map((i) => loop(i));
}
